package com.lyr.sockjs.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

/**
 * @author lyr
 */
@Controller
@Slf4j
public class WebsocketController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;


    @MessageMapping("/greeting")
        public String test(String tt){
            return LocalDateTime.now().toString()+":" + tt;
        }

    @MessageMapping("/hello")
    //@SendTo("/topic/greetings")
    public void greeting(User user) throws  Exception{
        TimeUnit.SECONDS.sleep(2);
        //手动去发送数据到客户端
        log.info("greeting接收到数据了"+user.getUsername());
        simpMessagingTemplate.convertAndSend("/topic/greetings","hello spring stomp 来啦来啦");
    }
}
